package cn.crism.blog.service.impl;

import cn.crism.blog.entity.Comment;
import cn.crism.blog.entity.Reply;
import cn.crism.blog.param.QueryParam;
import cn.crism.blog.repository.ReplyRepository;
import cn.crism.blog.service.CommentService;
import cn.crism.blog.service.ReplyService;
import cn.crism.blog.vo.ReplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @Author: Crism
 * @Date: 上午9:37 18/2/8
 * @Description:
 */

@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyRepository replyRepository;

    @Autowired
    private CommentService commentService;

    @Override
    public List<ReplyVO> getCommentReply(Comment comment){
        Sort sort = new Sort(Sort.Direction.ASC, "replyTime");
        List<Reply> replyList = replyRepository.findAllByCommentAndReplyStatus(comment,true,sort);
        return getReplys(replyList);
    }

    @Override
    public Integer getCount(Comment comment) {
        return replyRepository.countAllByComment(comment);
    }

    @Override
    public Boolean removeReply(String replyId) {
        if (replyId!=null){
            replyRepository.delete(replyId);
            return true;
        }
        return false;
    }

    public List<ReplyVO> getReplys(List<Reply> replyList){
        List<ReplyVO> replys = new ArrayList<>();
        if (replyList.size()>0){
            for (Reply reply : replyList) {
                replys.add(new ReplyVO(reply));
            }
        }
        return replys;
    }

    @Override
    public Boolean createReply(Reply reply,String commentId) {
        reply.setComment(commentService.findCommentById(commentId));
        try {
            replyRepository.save(reply);
        }catch (Exception e){
            return false;
        }finally {
            return true;
        }
    }

    @Override
    public Boolean accessReply(String replyId) {
        if (replyId!=null){
            Reply reply = replyRepository.findOne(replyId);
            reply.setReplyStatus(true);
            replyRepository.save(reply);
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Object> getReplySByComment(QueryParam queryParam) {
        Sort sort = new Sort(Sort.Direction.ASC, "replyTime");
        PageRequest pageable = new PageRequest(queryParam.getPage(), queryParam.getSize(),sort);
        Comment comment = commentService.findCommentById(queryParam.getMixParam());
        if (comment!=null){
            Page<Reply> replys = replyRepository.findAllByComment(comment,pageable);
            if (replys!=null){
                Map<String, Object> map = new HashMap<>();
                List<ReplyVO> replyVOS = new ArrayList<>();
                for (Reply reply:replys) {
                    replyVOS.add(new ReplyVO(reply));
                }
                map.put("totalElements", replys.getTotalElements());
                map.put("content", replyVOS);
                return map;
            }
        }
        return null;
    }
}
